package com.chen.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.chen.common.Result;
import com.chen.mapper.UserMapper;
import com.chen.pojo.User;
import com.chen.pojo.bean.UserInfo;
import com.chen.service.Impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "UserController", tags = "用户操作")
@RequestMapping("/user")
@SuppressWarnings("all")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/all")
    @ApiOperation("所有用户")
    public Result userAll() {
        return userService.userAll();
    }


    @GetMapping("/selectPage")
    @ApiOperation("分页")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "1") Integer status,
                           @RequestParam(defaultValue = "") String search) {
        return userService.findPage(pageNum, pageSize, status, search);
    }


    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PutMapping("/updateUser")
    @ApiOperation("更新")
    public Result updateUser(@RequestBody UserInfo user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation("删除")
    public Result deleteUser(@PathVariable("id") Long id) {
        return userService.deleteUser(id);
    }


    @PutMapping("/agree/{id}")
    @ApiOperation("审核")
    public Result<?> agree(User user) {
        return userService.agree(user);
    }

    @GetMapping("/findMember")
    @ApiOperation("查询男女比例")
    public Result<?>  findMember(){
        return  Result.success(userService.findMember());
    }

    @GetMapping("/findNumbers")
    @ApiOperation("查询每天注册的用户数")
    public Result<?> findNumbers(){
        return Result.success(userService.findNumbers());
    }




    /**
     * Excel导出
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = CollUtil.newArrayList();
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            Map<String, Object> row = new LinkedHashMap<>();
            row.put("用户名", user.getUsername());
            row.put("昵称", user.getNick());
            row.put("年龄", user.getAge());
            row.put("性别", user.getSex());
            row.put("地址", user.getAddress());
            row.put("注册时间", user.getRegtime());
            row.put("个人介绍",user.getIntroduce());
            list.add(row);
        }


        // 2. 写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }





}
